<?php

namespace App\Exceptions;

use Dingo\Api\Contract\Debug\ExceptionHandler;
use Dingo\Api\Exception\Handler as DingoHandler;
use Dingo\Api\Exception\ValidationHttpException;
use Illuminate\Auth\AuthenticationException;
class DingoExceptionHandler extends DingoHandler implements ExceptionHandler
{
    public function handle($exception)
    {
        $error = [
            'success'       => false,
            'message'       => ':message',
            'code'          => ':status_code',
            'data'          => [],
            'debug'         => ':debug',
        ];

        if ($exception instanceof ValidationHttpException) {
            $message = $exception->getErrors()->first();
            $error['message'] = $message;
        }

        if ($exception instanceof AuthenticationException) {
            $error['message'] = 'Token not provided';
            $error['code'] = 401;
        }

        if($exception instanceof WechatException) {
            $error['message'] = $exception->getMessage();
            $error['code'] = $exception -> getCode();
        }

        $this->setErrorFormat($error);

        return parent::handle($exception)->setStatusCode(200);
    }
}

